package com.guigu.aws.demo01;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;

/**
 * Hello world!
 *
 */
public class S3DownloadApp {

	private static String AWS_ACCESS_KEY = "AWS_ACCESS_KEY";
	private static String AWS_SECRET_KEY = "AWS_SECRET_KEY ";

//  文件下载
	public static void main(String[] args) throws IOException {
		// 创建 s3 对象
		AmazonS3 s3 = new AmazonS3Client(new BasicAWSCredentials(AWS_ACCESS_KEY, AWS_SECRET_KEY));
		// 设置区域 ( 我们使用的是宁夏区域 )
		s3.setRegion(Region.getRegion(Regions.CN_NORTHWEST_1));
		// 下载文件 ( 桶名，文件夹名+文件名 )
		S3Object object = s3.getObject(new GetObjectRequest("demo02", "img/wenti"));
		// 得到输入流
		S3ObjectInputStream input = object.getObjectContent();
		// 转换成字符流
		BufferedReader reader = new BufferedReader(new InputStreamReader(input));
		// 自定义输出流
		PrintWriter pw = new PrintWriter("C:\\Users\\86182\\Desktop\\s3inputfile\\myfile.txt");
		// 读这个流
		while (true) {
			String line = reader.readLine();
			if (line == null)
				break;
			pw.println(line);
		}
		System.out.println("写入成功...");
		reader.close();
		pw.close();
	}

}
